﻿<%@ WebHandler Language="C#"  Class="GovernmentOfficeProject.handler.ContactsDatafeed" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.SysTools;
using GovernmentOfficeProject.LevelBusinessLogic;
using System.Web.SessionState;

namespace GovernmentOfficeProject.handler
{
    /// <summary>
    /// ContactsDatafeed 的摘要说明
    /// </summary>
    public class ContactsDatafeed : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            //权限判断
            if (SystemContext.CurrentUserID == 0)
            {
                context.Response.Redirect("~/Login.htm?FromUrl=" + HttpUtility.UrlEncode(context.Request.Url.AbsoluteUri));
            }
            else
            {
                SystemContext.CurrentUserID = SystemContext.CurrentUserID;
            }
            context.Response.ContentType = "text/plain";
            string action = context.Request.Params["action"];
            string ret = "";
            switch (action)
            {
                case "list":ret =  getContentsData(); break;
                case "add": ret = saveOrUpdate(); break;
                case "edit": ret = saveOrUpdate(); break;
                case "delete": ret = delete(); break;
            }
            context.Response.Write(ret);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string getContentsData()
        {
            HttpContext context = System.Web.HttpContext.Current;
            BLL_Contacts bllContacts = new BLL_Contacts();
            int currentPage = Convert.ToInt16(context.Request.Params["page"]);
            int pageSize = Convert.ToInt16(context.Request.Params["rows"]);
            Entity_USER_Contacts contacts = new Entity_USER_Contacts();
            contacts.Name = context.Request.Params["Name"];
            contacts.MobileNumber = context.Request.Params["MobileNumber"];
            contacts.PhoneNumber = context.Request.Params["PhoneNumber"];
            contacts.Company = context.Request.Params["Company"];
            contacts.UserId = SystemContext.CurrentUserID;
            Grid<Entity_USER_Contacts> grid = bllContacts.getContactsGridData(contacts, pageSize, currentPage);
            return JsonHelper.ToJson(grid); ;
        }


        /// <summary>
        /// 保存或更新  
        /// </summary>
        /// <returns></returns>
        public string saveOrUpdate()
        {
            HttpContext context = System.Web.HttpContext.Current;
            BLL_Contacts bllContacts = new BLL_Contacts();
            Entity_USER_Contacts contacts = new Entity_USER_Contacts();
            bool isSuccess = true;
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            if (context.Request.Params["id"] != null)
            {
                contacts.Id = Convert.ToDecimal(context.Request.Params["id"]);
            }
            contacts.UserId = SystemContext.CurrentUserID;
            contacts.Name = context.Request.Params["Name"];
            contacts.MobileNumber = context.Request.Params["MobileNumber"];
            contacts.PhoneNumber = context.Request.Params["PhoneNumber"];
            contacts.FaxNumber = context.Request.Params["FaxNumber"];
            contacts.Company = context.Request.Params["Company"];
            contacts.Email = context.Request.Params["Email"];
            contacts.Address = context.Request.Params["Address"];
            contacts.Remark = context.Request.Params["Remark"];

            if (bllContacts.validationMobileNumber(contacts.MobileNumber, contacts.Id))
            {
                ht.Add("IsSuccess", false);
                ht.Add("Msg", "保存失败，手机号码重复！");
                return JsonHelper.ToJson(ht);
            }

            isSuccess = contacts.Id == 0 ? bllContacts.saveContacts(contacts) : bllContacts.updateContactsById(contacts);
           
            if(isSuccess)
            {
                ht.Add("IsSuccess", true);
                ht.Add("Msg", "保存成功！");
            }
            else
            {
                ht.Add("IsSuccess", true);
                ht.Add("Msg", "保存失败！");
            }

            return JsonHelper.ToJson(ht);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public string delete()
        {
            HttpContext context = System.Web.HttpContext.Current;
            BLL_Contacts bllContacts = new BLL_Contacts();
            decimal id = Convert.ToDecimal(context.Request.Params["id"]);
            return JsonHelper.ToJson(bllContacts.deleteContactsById(id));
        }
        
       

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}